<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Supplier;


use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\Good;
use App\Models\GoodsOneType;
use App\Models\GoodsTwoType;
use App\Models\IntegralGood;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class SupplierController extends Controller
{
    private $namespace = "App\Http\Controllers\Api\V1\Manager\Supplier\SupplierController";

    public function index(Request $request)
    {
        $status = $request->status;
        $suppname = $request->suppname;
        $phone = $request->phone;
        $djrname = $request->djrname;
        $createStartTime = $request->createStartTime;
        $createEndTime = $request->createEndTime;
        $endStartTime = $request->endStartTime;
        $endEndTime = $request->endEndTime;
        $pageSize = $request->pageSize;
        $sql = Supplier::select('id', 'suppname', 'djrname','suppname', 'phone', 'status', 'createtime', 'jztime', 'updatetime', 'admin_name')
            ->where('is_del', 0);

        if (isset($status)) {
            if($status == 6){
                $sql->where('status',1)->where('jztime','<=',strtotime(date("Y-m-d H:i:s",strtotime("+7 Day"))));
            }else{
                $sql->where('status',$status);
            }
        }
        if (isset($suppname)) {
            $sql->where('suppname', 'like', "%$suppname%");
        }
        if (isset($phone)) {
            $sql->where('phone', 'like', "%$phone%");
        }
        if (isset($djrname)) {
            $sql->where('djrname', 'like', "%$djrname%");
        }
        if (isset($createStartTime)) {
            $sql->where('createtime', '>=', $createStartTime);
        }
        if (isset($createEndTime)) {
            $sql->where('createtime', '<=', $createEndTime);
        }
        if (isset($endStartTime)) {
            $sql->where('jztime', '>=', $endStartTime);
        }
        if (isset($endEndTime)) {
            $sql->where('jztime', '<=', $endEndTime);
        }
        $supplierList = $sql->orderByDesc('updatetime')
            ->paginate($pageSize)
            ->toArray();
        $data['supplierList'] = $supplierList['data'];
        foreach ($data['supplierList'] as $key => $value) {
            $isAbout = 0;
            if($value['status'] == 1){
                if ($value['jztime'] < strtotime(date("Y-m-d H:i:s",strtotime("+7 Day")))){
                    $isAbout = 1;
                }
            }
            $data['supplierList'][$key]['isAbout'] = $isAbout;
            if (isset($value['createtime'])) {
                $data['supplierList'][$key]['createtime'] = date("Y-m-d H:i:s", $value['createtime']);
            }
            if (isset($value['jztime'])) {
                $data['supplierList'][$key]['jztime'] = date("Y-m-d H:i:s", $value['jztime']);
            }
            if (isset($value['updatetime'])) {
                $data['supplierList'][$key]['updatetime'] = date("Y-m-d H:i:s", $value['updatetime']);
            }
        }
        $data['totalNum'] = $supplierList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $supplier = Supplier::where('id', $id)->first();
        if ($supplier) {
            if (isset($supplier['createtime'])) {
                $supplier['createtime'] = date("Y-m-d H:i:s", $supplier['createtime']);
            }
            if (isset($supplier['jztime'])) {
                $supplier['jztime'] = date("Y-m-d H:i:s", $supplier['jztime']);
            }
            if (isset($supplier['updatetime'])) {
                $supplier['updatetime'] = date("Y-m-d H:i:s", $supplier['updatetime']);
            }
        }
        return $this->apiResponse($supplier);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('suppname', 'djrname', 'phone', 'createtime', 'jztime');
        if (!isset($data['suppname']) || !isset($data['phone']) || !isset($data['createtime']) || !isset($data['jztime'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        if((strtotime($data['jztime']) <= strtotime($data['createtime'])) || (strtotime($data['jztime']) <= strtotime(date("Y-m-d H:i:s")))){
            return $this->apiResponse('', config('errorLiu.GONGYINGSHANGSHIJIANDBUDUIDE'));
        }
        $data['admin_name'] = $request->ADMINUSERNAME;
        $data['updatetime'] = time();
        $data['createtime'] = strtotime($data['createtime']);
        $data['jztime'] = strtotime($data['jztime']);
        $num = Supplier::where('suppname', $data['suppname'])->where('id','!=',$id)->where('is_del', 0)->count();
        if($num){
            return $this->apiResponse('', config('errorLiu.YICUNZAIODEGONGYINGSHANG'));
        }
        if ($id) {
            $content = "修改";
            Supplier::where('id', $id)->update($data);
        } else {
            $content = "新增";
            $id = Supplier::insertGetId($data);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的供应商,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "供应商模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $supplier = Supplier::where('id', $id)->first();
        Supplier::where('id', $id)->update(['is_del' => 1, 'status' => 3,'admin_name'=>$request->ADMINUSERNAME]);
        Good::where('supplier_id', $id)->update([
            'old_status' => \DB::raw('status'),
            'status' => 2,
            'is_del' => 1
        ]);
        IntegralGood::where('supplier_id', $id)->update([
            'old_status' => \DB::raw('status'),
            'status' => 2,
            'is_del' => 1
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的供应商,内容为【".json_encode($supplier)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "供应商模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function supplierEnd(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Supplier::where('id', $id)->update(['status' => 3,'admin_name'=>$request->ADMINUSERNAME]);
        Good::where('supplier_id', $id)->update([
            'old_status' => \DB::raw('status'),
            'status' => 2
        ]);
        IntegralGood::where('supplier_id', $id)->update([
            'old_status' => \DB::raw('status'),
            'status' => 2
        ]);
        //添加管理员日志开始
        $url = $this->namespace . "/supplierEnd";
        $title = "修改";
        $content = "终止了id为【".$id."】的供应商的合作";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "供应商模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        Supplier::where('id', $id)->update(['status' => 1,'admin_name'=>$request->ADMINUSERNAME]);
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content = "继续了id为【".$id."】的供应商的合作";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "供应商模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}